
#include <jni.h>

#include "utility.h"

// #include <android/log.h>
// #define LOG_TAG "CPPPPChannelManagement"
// #define Log(fmt, args...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, fmt, ##args)
// #define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, fmt, ##args)
// #define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, fmt, ##args)
// #define SAFE_DELETE(p) do{ delete p; p=NULL; } while(false)


#include <utility>

#include "DeviceManagement.h"

CDeviceManagement::CDeviceManagement()
{
	memset(&m_device, 0 ,sizeof(m_device));
}

CDeviceManagement::~CDeviceManagement()
{   
	StopAll();
}
void CDeviceManagement::StopAll()
{
	mLock.Lock();
	int i;
	for(i = 0; i < MAX_DEVICE_NUM; i++)
	{
		if(m_device[i].bValid == 1)
		{            
			m_device[i].dev->Logout();
		}
	}  

	PPPP_Connect_Break();
	memset(&m_device, 0 ,sizeof(m_device));
 
	mLock.Unlock();
}


/************************************************************************/
/*                                                                      */
/************************************************************************/
int CDeviceManagement::Login(char * szDID, char *user, char *pwd)
{

	mLock.Lock();

	int i;
	for(i = 0; i < MAX_DEVICE_NUM; i++)
	{
		if(m_device[i].bValid == 1 && strcmp(m_device[i].szDID, szDID) == 0)
		{
			m_device[i].dev->Logout();
		}
	}  
	
	for(i = 0; i < MAX_DEVICE_NUM; i++)
	{
		if(m_device[i].bValid == 0)
		{
			m_device[i].bValid = 1;            
			strcpy(m_device[i].szDID, szDID);      
			//CCircleBuf *pVideoBuf = new CCircleBuf();
			//pVideoBuf->Create(VBUF_SIZE);
			//m_PPPPChannel[i].pVideoBuf = pVideoBuf;    

			//CCircleBuf *pPlaybackVideoBuf = new CCircleBuf();
			//m_PPPPChannel[i].pPlaybackVideoBuf = pPlaybackVideoBuf;
			m_device[i].dev = new CDevice(szDID, user, pwd);
			m_device[i].dev->Login(i);
			//mLock.Unlock();
			//return 1;
			break;
		}
	}
	mLock.Unlock();
	return (i>=MAX_DEVICE_NUM)?-1:i;
}

int CDeviceManagement::Logout(int id)
{
	if ((id<0)||(id>MAX_DEVICE_NUM)){return -1;}
	if(m_device[id].bValid == 0){return -1;}

	mLock.Lock();
	int i = id;

	m_device[i].dev->Logout();
	//memset(m_device[i].szDID, 0, sizeof(m_device[i].szDID));
	SAFE_DELETE(m_device[i].dev);       
	//SAFE_DELETE(m_device[i].pVideoBuf);  
	//SAFE_DELETE(m_device[i].pPlaybackVideoBuf);
	m_device[i].bValid = 0;

	mLock.Unlock();
	return 0;
}



/************************************************************************/
/*                                                                      */
/************************************************************************/
int CDeviceManagement::RealPlay(int id, int streamid)
{
	if ((id<0)||(id>MAX_DEVICE_NUM)){return -1;}
	if(m_device[id].bValid == 0){return -1;}

	mLock.Lock();
	int ret = m_device[id].dev->RealPlay(streamid);
	mLock.Unlock();

	return ret;
}

int CDeviceManagement::StopPlay(int id)
{
	if ((id<0)||(id>MAX_DEVICE_NUM)){return -1;}
if(m_device[id].bValid == 0){return -1;}

	mLock.Lock();
	int ret = m_device[id].dev->StopPlay();
	mLock.Unlock();

	return ret;
}


/************************************************************************/
/*                                                                      */
/************************************************************************/
int CDeviceManagement::StartAudio(int id)
{
	if ((id<0)||(id>MAX_DEVICE_NUM)){return -1;}
	if(m_device[id].bValid == 0){return -1;}

	mLock.Lock();
	int ret = 0;// m_device[id].dev->StartAudio();
	mLock.Unlock();

	return ret;
}

int CDeviceManagement::StopAudio(int id)
{
	if ((id<0)||(id>MAX_DEVICE_NUM)){return -1;}
	if(m_device[id].bValid == 0){return -1;}

	mLock.Lock();
	int ret = 0;//m_device[id].dev->StopAudio();
	mLock.Unlock();

	return ret;
}


/************************************************************************/
/*                                                                      */
/************************************************************************/
int CDeviceManagement::StartTalk(int id)
{
	if ((id<0)||(id>MAX_DEVICE_NUM)){return -1;}
	if(m_device[id].bValid == 0){return -1;}

	mLock.Lock();
	int ret = 0;//m_device[id].dev->StartTalk();
	mLock.Unlock();

	return 0;
}

int CDeviceManagement::StopTalk(int id)
{
	if ((id<0)||(id>MAX_DEVICE_NUM)){return -1;}
	if(m_device[id].bValid == 0){return -1;}

	mLock.Lock();
	int ret = 0;//m_device[id].dev->StopTalk();
	mLock.Unlock();

	return ret;
}

int CDeviceManagement::TalkAudioData(int id,char * audioPCM,int len)
{
	if ((id<0)||(id>MAX_DEVICE_NUM)){return -1;}
	if(m_device[id].bValid == 0){return -1;}

	mLock.Lock();
	int ret = 0;//m_device[id].dev->TalkAudioData(audioPCM, len);
	mLock.Unlock();

	return ret;
}


/************************************************************************/
/*                                                                      */
/************************************************************************/

int CDeviceManagement::PTZControl(int id,int command, int value)
{
	if ((id<0)||(id>MAX_DEVICE_NUM)){return -1;}
	if(m_device[id].bValid == 0){return -1;}

	mLock.Lock();
	int ret = m_device[id].dev->PTZControl(command);
	mLock.Unlock();

	return ret;
}

/*
#include <map>
using namespace std;

template<typename K, typename T>
class a
{
public:
map<K,T> _map;
T find(K v)
{
typename map<K,T>::iterator iter;//mark
iter = _map.find(v);
if(iter != _map.end())
return iter->second;
else
return (T)0;
}
};
int main()
{
map<int, unsigned int> b;
return 0;
}
*/
